package com.sgj.common.vaild;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @Author: Guoji Shen
 * @Date: 2021/6/28 17:30
 */
public class ListValueConstraintVaildator implements ConstraintValidator<ListValue, Integer> {

    private Set<Integer> set = new HashSet<Integer>();

    // 初始化方法
    public void initialize(ListValue constraintAnnotation) {
        int[] values = constraintAnnotation.value();
        for (int value : values) {
            if (Objects.nonNull(value)) {
                set.add(value);
            }
        }
        // ConstraintValidator.super.initialize(constraintAnnotation);
    }

    // 判断是否校验成功
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return set.contains(value);
    }
}